package com.kotlin.android.ktx.ext

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes

/**
 * 状态选择器扩展
 *
 * Created on 2020/6/9.
 *
 * @author o.s
 */

/**
 * 颜色状态表
 */
fun getColorStateList(
    @ColorInt normalColor: Int,
    @ColorInt pressColor: Int,
    @ColorInt selectedColor: Int? = null,
    @ColorInt disableColor: Int? = null
): ColorStateList {
    var size = 4
    if (disableColor == null) {
        size -= 1
    }
    if (selectedColor == null) {
        size -= 1
    }

    //初始化，默认可用样式
    val states = Array(size) { State.ENABLE.value }
    val colors = IntArray(size) { normalColor }

    // 按压效果
    states[1] = State.PRESSED.value
    colors[1] = pressColor

    if (selectedColor != null && size >= 3) {
        // 选中效果
        states[2] = State.SELECTED.value
        colors[2] = selectedColor
        if (disableColor != null && size >= 4) {
            // 不可用效果
            states[3] = State.DISABLE.value
            colors[3] = disableColor
        }
    } else {
        if (disableColor != null && size >= 3) {
            states[2] = State.DISABLE.value
            colors[2] = disableColor
        }
    }

    return ColorStateList(states, colors)
}

/**
 * Drawable状态表
 */
fun getStateListDrawable(
    normal: Drawable,
    pressed: Drawable?,
    selected: Drawable?,
    disable: Drawable?
): StateListDrawable {
    val sld = StateListDrawable()
    pressed?.run {
        sld.addState(State.PRESSED.value, this)
    }
    selected?.run {
        sld.addState(State.SELECTED.value, this)
    }
    disable?.run {
        sld.addState(State.DISABLE.value, this)
    }
    sld.addState(State.ENABLE.value, normal)
    return sld
}

/**
 * Drawable状态表
 */
fun Context.getStateListDrawable(
    @DrawableRes normal: Int,
    @DrawableRes pressed: Int?,
    @DrawableRes selected: Int?,
    @DrawableRes disable: Int?
): StateListDrawable {
    return getDrawable(normal)?.let {
        getStateListDrawable(it, getDrawable(pressed), getDrawable(selected), getDrawable(disable))
    } ?: StateListDrawable()
}

/**
 * Drawable状态表
 */
fun View.getStateListDrawable(
    @DrawableRes normal: Int,
    @DrawableRes pressed: Int?,
    @DrawableRes selected: Int?,
    @DrawableRes disable: Int?
): StateListDrawable {
    return getDrawable(normal)?.let {
        getStateListDrawable(it, getDrawable(pressed), getDrawable(selected), getDrawable(disable))
    } ?: StateListDrawable()
}

/**
 * State状态类型枚举
 */
enum class State(val value: IntArray) {
    DISABLE(intArrayOf()),
    ENABLE(intArrayOf(android.R.attr.state_enabled)),
    PRESSED(intArrayOf(android.R.attr.state_pressed, android.R.attr.state_enabled)),
    SELECTED(intArrayOf(android.R.attr.state_selected, android.R.attr.state_enabled)),
    CHECKED(intArrayOf(android.R.attr.state_checked, android.R.attr.state_enabled)),
}